<?php
  class Core_AutoComplete{
  //class Core_FormException extends Exception{
  //class Core_Commons{			
  	/**
     * @shortdesc Contains HTML source for AutoComplete
     * @type String
     * @public
     * @default value : 3
     */
    var $html_src;
    
    /**
     * @shortdesc Location (Url) of File containing inputs values
     * @type String
     * @public
     * @default value : "" (read from current dir)
     */
    var $file_location;
    
    /**
     * @shortdesc Name of file
     * @type String
     * @public
     */
    var $file_name;
    
    /**
     * @shortdesc Delimiter of Input params at Input file
     * @type Char
     * @public
     * @default value : "\n" (Line Feed)
     */
    var $delimiter;
    
    /**
     * @shortdesc Javascript Source of Auto Complete (to be places withing the <HEAD></HEAD> section)
     * @type String
     * @public
     */ 
    var $js_string;
    
    /**
     * @shortdesc Width of Form
     * @type Integer
     * @public
     * @default value : 300
     */
    var $form_width;
    
    /**
     * @shortdesc Read Input values from Session or not (read from session in case the input list is static in order to increase performance)
     * 0 : dont read from session
     * 1 : read from session (default)
     * @type Integer
     * @public
     * @default value : 1
     **/
    var $read_from_session;
    
    var $form_html_string;
    
    public function __construct($fileName, $readFromSession = 1, $delimiter = "\n", $fileLocation = "", $formWidth = 300, $selColor = "green", $UnSelColor = "white"){
      $this->file_location = $fileLocation;
      
      $this->file_name = $fileName;
      
      $this->delimiter = $delimiter;
      
      $this->form_width = $formWidth;
      
      $this->read_from_session = $readFromSession; 
      
      /*echo "<pre>";
      print_r($this->file_name);*/
      
      $this->js_string  = '
        <script language="JavaScript">
          var req;
          
          function loadXMLDoc(url){
            // Internet Explorer
            try{ 
              req = new ActiveXObject("Msxml2.XMLHTTP"); 
            }
            catch(e){
              try{ 
                req = new ActiveXObject("Microsoft.XMLHTTP"); 
              }
              catch(oc){ 
                req = null; 
              }
            }
            
            // Mozailla/Safari
            if(!req && typeof XMLHttpRequest != "undefined"){ 
              req = new XMLHttpRequest(); 
            }
            
            // Call the processChange() function when the page has loaded
            if(req != null){
              req.onreadystatechange = processChange;
              
              req.open("GET", url, true);
              
              req.send(null);
            }
          }
          
          function processChange(){
            // The page has loaded and the HTTP status code is 200 OK
            if(req.readyState == 4 && req.status == 200){
              // Write the contents of this URL to the searchResult layer
              if(req != null)
                getObject("searchResult").innerHTML = req.responseText;
            }
          }

          function getObject(name){
            var ns4 = (document.layers) ? true : false;
            
            var w3c = (document.getElementById) ? true : false;
            
            var ie4 = (document.all) ? true : false;
          
            if (ns4) return eval(\'document.\' + name);
            
            if (w3c) return document.getElementById(name);
             
            if (ie4) return eval(\'document.all.\' + name);
             
            return false;
          }
          
          window.onload = function() {
            getObject("q").focus();
          }
          
          function setit(str){
            document.getElementById("searchResult").style.visibility.value = "visible";
            
            document.all.q.value=str; 
          }
        </script> 
      ';
      
      $quote = "'";
      
      if($fileLocation == "")
        $fileLocation = "default";
        
      if ($delimiter == "\n")
        $delimiter = "default";
        
      /*$RedirectCall = 'find.php?fn='.$fileName.'&fl='.$fileLocation.'&d='.$delimiter.'&sc='.$selColor.'&usc='.$UnSelColor.'&rfs='.$readFromSession;
      
      $this->FormHTMLString = '
        <form action="'.$RedirectCall.'" method="GET" target="searchWindow">
          <input tabindex="1" type="text" name="q" id="q" size="20" onkeyup="searchResult.style.visibility=\'visible\';loadXMLDoc(\''.$RedirectCall.'&q='.$quote.'+this.value)" style="width:'.$formWidth.'px;" value="">
          <div onclick="this.innerHTML=\'\';this.style.visibility=\'hidden\';" tabindex="2" id="searchResult" name="searchResult" style="visibility:visible;font-family:Arial; font-size:12px; width:'.$formWidth.'px; border:#000000 solid 1px; padding:3px;background-color:'.$UnSelColor.'" ></div>
        </form> 
      ';*/
        
      echo $RedirectCall = 'find/fn/' . $fileName . '/fl/' . $fileLocation . '/d/' . $delimiter . '/sc/' . $selColor . '/usc/' . $UnSelColor . '/rfs/' . $readFromSession;
      
      $this->form_html_string = '
        <form action="'.$RedirectCall.'" method="GET" target="searchWindow">
          <input tabindex="1" type="text" name="q" id="q" size="20" onkeyup="searchResult.style.visibility=\'visible\'; loadXMLDoc(\'' . $RedirectCall . '/q/' . $quote . '+this.value)" style="width:' . $formWidth . 'px;" value="">
          <div onclick="this.innerHTML=\'\';this.style.visibility=\'hidden\';" tabindex="2" id="searchResult" name="searchResult" style="visibility:visible;font-family:Arial; font-size:12px; width:' . $formWidth . 'px; border:#000000 solid 1px; padding:3px;background-color:' . $UnSelColor . '" ></div>
        </form> 
      ';
    	
    }
    
    // Desc : Output Javascript Source 
    public function jsSrc(){
      return $this->js_string;
    } 
    
    // Desc : Output HTML Source
    public function htmlSrc(){
      return $this->form_html_string;
    }
  }
?>  